package com.sample.generator.impl;

import java.util.Map;

import com.sample.generator.Generator;
import com.sample.generator.config.WriterConfig;
import com.sample.generator.context.GenerationContext;
import com.sample.generator.template.TemplateProcessor;

public class DefaultGenerator implements Generator {

	private TemplateProcessor templateProcessor;

	protected DefaultGenerator(TemplateProcessor templateProcessor) {

		if (templateProcessor == null) {
			throw new IllegalArgumentException(
					"Argument templateProcessor is null.");
		}
		this.templateProcessor = templateProcessor;
	}

	public void generate(GenerationContext... contexts) {

		if (contexts == null) {
			throw new IllegalArgumentException("Argument contexts is null.");
		}

		for (GenerationContext context : contexts) {
			generate(context);
		}
	}

	public void generate(GenerationContext context) {

		if (context == null) {
			throw new IllegalArgumentException("Argument contexts is null.");
		}

		Map<String, Object> contextMap = context.getContextMap();
		for (WriterConfig config : context.getWriterConfigs()) {
			templateProcessor.process(contextMap, config);
		}
	}
}
